package com.javafactroy.kunlong.codefactroy.service.tool;


import com.javafactroy.kunlong.base.config.datasource.DatasourceConfig;
import com.javafactroy.kunlong.base.config.datasource.oracle.OracleDatasourceConfig;
import com.javafactroy.kunlong.base.metadata.model.TMetadataModelOracle;
import com.javafactroy.kunlong.base.metadata.valueobj.MetadataTable;
import com.javafactroy.kunlong.codefactroy.consts.IdbType;
import com.javafactroy.kunlong.codefactroy.convert.OracleTablesConvert;
import com.javafactroy.kunlong.codefactroy.service.base.FileTemplateFactroy;
import com.javafactroy.kunlong.codefactroy.service.domain.valueobj.EntityProps;
import com.javafactroy.kunlong.data.dao.oracle.OracleTablesMapper;
import com.javafactroy.kunlong.data.entity.mysql.MysqlTables;
import com.javafactroy.kunlong.data.entity.oracle.OracleTables;
import com.javafactroy.kunlong.common.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
//@Service
@ConditionalOnProperty(prefix = "spring.datasource.postgres", name = "enabled", havingValue = "true")
public class DbCodeOracleTool {
    Boolean rmFile = true;
    //@Autowired
    //TMetadataModelOracle metadataModel;

    @Autowired
    OracleTablesConvert oracleTablesConvert;
    @Autowired
    FileTemplateFactroy fileTemplateFactroy;
    @Autowired
    OracleDatasourceConfig oracleDatasourceConfig;

    @Autowired
    OracleTablesMapper oracleTablesMapper;

    void make(DatasourceConfig datasourceConfig){
        if(datasourceConfig.getDbName().equals(IdbType.DbType_Oracle)) {
            makes();
        }
    }

    public void makes() {
        //information_schema.TABLES

        loadTables().forEach(i -> make(i.getTableName()));
    }

    public void make(String table) {

        //MetadataTable metadata = metadataModel.loadDbPostgresMetadata(table);
        //makeFile(metadata);
    }

    void makeFile(MetadataTable metadata) {
        EntityProps entityProps = metadata.toEntityProps();
        entityProps.makeParams(IdbType.DbType_PostGres);
        log.info("template params={}", CommonUtils.toJSONString(entityProps.getStringTemplate().getParams()));

        if (rmFile) {
            fileTemplateFactroy.rmDbFile(metadata);
            rmFile = false;
        }
        fileTemplateFactroy.getList().forEach(i -> {

            String stext = i.valueOf(entityProps).parse(i.readFile());
            i.writeFile(stext);
        });

    }


    List<MysqlTables> loadTables() {
        String dbName = oracleDatasourceConfig.getDbName();
        List<OracleTables> tables = oracleTablesMapper
                .select(new OracleTables().setDbName("public").setTableCatalog(dbName));
        log.info("tables = {}", CommonUtils.toJSONString(tables));

        return oracleTablesConvert.fromList(tables);
    }

}
